package com.lsh.oms.core.service.bills.impl;

import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.model.bills.MessageBean;
import com.lsh.oms.core.service.bills.BaseHandlerKafkaMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author xuzhan
 * @date 16/11/4
 */

@Transactional(rollbackFor=Exception.class)
@Slf4j
public abstract class AbstractKafkaMessageService<T> implements BaseHandlerKafkaMessage {

    public abstract void doBiz(T obj);

    @Override
    public void handleKafkaMessage(MessageBean messageBean) throws Exception{
        try {

            this.doBiz(this.parse(messageBean.getMessage()));
            log.info("AbstractKafkaMessageService : doBiz end.");
        }catch (BusinessException e){
            log.error("AbstractKafkaMessageService ::: BusinessException:", e);
            throw new BusinessException(e.getCode(),e.getMessage(),e);
        }catch (Exception e){
            log.error("AbstractKafkaMessageService ::: Exception:", e);
            throw new BusinessException(ReturnCode.UPDATE_FAIL,"handleKafkaMessage exception ",e);
        }
    }

    public abstract T parse(String json) throws Exception;

}
